﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using NRCP.BusinessLogicLayer;
using NRCP.Helpers;

namespace NRCP.MetaData
{
    public partial class Chemical_List : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadData();
            }
        }

        private void LoadData()
        {
            BindGrid(0, SiteSettings.PageSize);
            BindPager(0, SiteSettings.PageSize);
        }

        protected void repPager_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            BindGrid(Convert.ToInt32(e.CommandArgument) - 1, SiteSettings.PageSize);
        }

        private void BindPager(int currentPageIndex, int pageSize)
        {
            ChemicalBLL bll = new ChemicalBLL();
            Int32 totalCount = 0;
            Int32 pageCount = 0;
            totalCount = bll.GetTotalCountForAllChemicals("Id", currentPageIndex, pageSize);
            pageCount = Convert.ToInt32(totalCount / pageSize);
            if (pageCount <= 0)
            {
                pageCount = 1;
            }

            litTotalRecordCount.Text = totalCount.ToString();
            repPager.DataSource = Enumerable.Range(1, pageCount).ToList();
            repPager.DataBind();
        }

        private void BindGrid(int currentPageIndex, int pageSize)
        {
            ChemicalBLL bll = new ChemicalBLL();
            grvList.DataSource = bll.GetAllChemicalsPaged("Id", currentPageIndex, pageSize);
            grvList.DataBind();
        }
    }
}